package com.jims.api.v1.controller.sync;

        import com.baomidou.kisso.my.controller.BaseController;

        import com.jims.master.common.data.Message;
        import com.jims.master.common.utils.SecurityUtil;
        import com.jims.master.common.web.impl.BaseDto;
        import com.jims.master.modules.sync.entity.*;
        import com.jims.master.modules.sync.service.*;
        import com.jims.master.modules.sys.entity.OrgDept;
        import com.jims.master.modules.sys.entity.PerformFreqDict;
        import com.jims.master.modules.sys.entity.SysOrgDict;
        import com.jims.master.modules.sys.service.OrgDeptService;
        import com.jims.master.modules.sys.service.PerformFreqDictService;
        import com.jims.master.modules.sys.service.SysOrgDictService;
        import com.jims.master.modules.sys.utils.DictUtils;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.bind.annotation.RestController;

        import java.util.List;
        import java.util.Map;

/**
 * Created by Administrator on 2017/11/16 0016.
 */
@RestController
@RequestMapping(value = "/prescription")
public class PrescriptionController extends BaseController {
    @Autowired
    private PrescriptionEnService prescriptionEnService;
    @Autowired
    private DrugDictService drugDictService;
    @Autowired
    private PrescriptionEnDetailsService prescriptionEnDetailsService;
    @Autowired
    private ClinicMasterService clinicMasterService;
    @Autowired
    private SysOrgDictService sysOrgDictService;
    @Autowired
    private PerformFreqDictService performFreqDictService;
    @Autowired
    private OrgDeptService orgDeptService;
    /**
     * @Title:  prescriptionEn
     * @Description: 查询个人处方
     * @param clinicId
     * @param clinicId
     * @return java.lang.String
     * @author CT
     * @date 2017/11/16 0016
     * @throws
     */
    @RequestMapping(value = "V100/prescriptionEn",method ={RequestMethod.GET,RequestMethod.POST})
    public String prescriptionEnV100(String clinicId){
	/*	Map<String, Object> map = new HashMap<String, Object>();*/
		/*map.put("registList",registList);*/
        PrescriptionEn prescriptionEn = new PrescriptionEn();
        prescriptionEn.setClinicId(clinicId);
        //prescriptionEn.setVisitId(visitId);
        List<PrescriptionEn> prescriptionEnList= prescriptionEnService.getByBillingId(prescriptionEn);
        if(prescriptionEnList.size()<1){
            prescriptionEnList = null;
        }
        Message mes = new Message();
        mes.setData(prescriptionEnList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title:  savePrescriptionEn
     * @Description: 保存个人处方
     * @return java.lang.String
     * @author CT
     * @date 2018/03/20 0016
     * @throws
     */
    @RequestMapping(value = "V100/savePrescriptionEn",method ={RequestMethod.GET,RequestMethod.POST})
    public String savePrescriptionEnV100(@RequestParam(required = true) String clinicId){
	/*	Map<String, Object> map = new HashMap<String, Object>();*/
		/*map.put("registList",registList);*/
        ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
        PrescriptionEn prescriptionEn = new PrescriptionEn();
        prescriptionEn.setClinicId(clinicId);
        //双滦西药房执行科室
        if(clinicMaster.getOrgId().equals("861100292")){
            OrgDept orgDept = orgDeptService.getByName(clinicMaster.getOrgId(),"西药房");
            prescriptionEn.setImplementDept(orgDept.getId());
        }
        String testNo = "CF" + clinicMaster.getClinicNo() + (int) (Math.random() * 9000);
        prescriptionEn.setBillingNum(testNo);
        prescriptionEnService.save(prescriptionEn);
        Message mes = new Message();
        mes.setData(prescriptionEn.getId());
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 删除个人处方
     * @param id
     * @author CT
     * @return
     */
    @RequestMapping(value = "V100/deletePrescriptionEn",method ={RequestMethod.GET,RequestMethod.POST})
    public String deletePrescriptionEnV100(@RequestParam(required = true) String id){
	/*	Map<String, Object> map = new HashMap<String, Object>();*/
		/*map.put("registList",registList);*/
        PrescriptionEn prescriptionEn = new PrescriptionEn();
        prescriptionEn.setId(id);
        int i = prescriptionEnService.delete(prescriptionEn);
        Message mes = new Message();
        mes.setData(i);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title:  selectDrug
     * @Description: 根据名称查药品
     * @param drugName
     * @return java.lang.String
     * @author CT
     * @date 2018/03/21 0016
     * @throws
     */
    @RequestMapping(value = "V100/selectDrug",method ={RequestMethod.GET,RequestMethod.POST})
    public String selectDrugV100(String drugName,String orgId){
	/*	Map<String, Object> map = new HashMap<String, Object>();*/
		/*map.put("registList",registList);*/
        DrugDict drugDict = new DrugDict();
        drugDict.setDrugName(drugName);
        drugDict.setOrgId(orgId);
        List<BaseDto> drugDictList = drugDictService.selectDrug(drugDict);
        Message mes = new Message();
        mes.setData(drugDictList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 获取药品途径
     * @param type
     * @author CT
     * @return
     */
    @RequestMapping(value = "V100/selectChannel",method ={RequestMethod.GET,RequestMethod.POST})
    public String selectChannel(String type){
        List<SysOrgDict> sysOrgDictList = sysOrgDictService.getByType(type);
        Message mes = new Message();
        mes.setData(sysOrgDictList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 获取频次字典表
     * @author CT
     * @return
     */
    @RequestMapping(value = "V100/freqDict",method ={RequestMethod.GET,RequestMethod.POST})
    public String freqDict(){
        List<PerformFreqDict> freqDictList = performFreqDictService.freqDictList();
        Message mes = new Message();
        mes.setData(freqDictList);
        return SecurityUtil.encryptMsgData(mes);
    }


    /**
     * 保存处方药品
     * @param map
     * @author CT
     * @return
     */
    @RequestMapping(value = "V100/saveDrug",method ={RequestMethod.GET,RequestMethod.POST})
    public String saveDrugV100(@RequestParam(required = true) Map map){
	/*	Map<String, Object> map = new HashMap<String, Object>();*/
		/*map.put("registList",registList);*/
        PrescriptionEnDetails prescriptionEnDetails = new PrescriptionEnDetails();
        prescriptionEnDetails.setDrugName((String) map.get("drugname"));//名称
        prescriptionEnDetails.setBillingId((String) map.get("billingId"));//主表id
        prescriptionEnDetails.setPackageSpec((String) map.get("packagespec"));//药品包装单位
        prescriptionEnDetails.setPackageUnit((String) map.get("packageunit"));//药品包装单位字典值
        prescriptionEnDetails.setSpec((String) map.get("minspec"));//最小包装规格
        prescriptionEnDetails.setFreqDesc((String) map.get("frequency"));//药物使用频次代码
        prescriptionEnDetails.setDrugCode((String) map.get("drugcode"));//药物剂型代码
        prescriptionEnDetails.setUnit((String) map.get("doseperunit"));//用量单位 -字典label值
        //prescriptionEnDetails.setSpec((String) map.get("once"));
        prescriptionEnDetails.setAdministration((String) map.get("channel"));//途径
        prescriptionEnDetails.setFirmid((String) map.get("firmid"));//药品厂商id
        prescriptionEnDetails.setFirmname((String) map.get("firmname"));//药品厂商名称
        prescriptionEnDetails.setPriceId((String) map.get("price_id"));//价表id
        prescriptionEnDetails.setPrice((String) map.get("price"));//药品单价
        prescriptionEnDetails.setNum((String) map.get("num"));//药品总数量
        prescriptionEnDetails.setRepetition("1");//剂数
        prescriptionEnDetails.setSingleDosage((String) map.get("singleDosage"));//单次用量
        prescriptionEnDetails.setSingleUnit((String) map.get("doseunits"));//药品单位
        prescriptionEnDetails.setAbidance((String) map.get("abidance"));//用药天数
        //prescriptionEnDetails.setAdministration((String) map.get("once"));
        int i = prescriptionEnDetailsService.save(prescriptionEnDetails);
        Message mes = new Message();
        mes.setData(i);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 删除处方药品
     * @author CT
     * @return
     */
    @RequestMapping(value = "V100/deleteDrug",method ={RequestMethod.GET,RequestMethod.POST})
    public String deleteDrugV100(String id){
	/*	Map<String, Object> map = new HashMap<String, Object>();*/
		/*map.put("registList",registList);*/
        PrescriptionEnDetails prescriptionEnDetails = new PrescriptionEnDetails();
        prescriptionEnDetails.setId(id);
        int i = prescriptionEnDetailsService.delete(prescriptionEnDetails);
        Message mes = new Message();
        mes.setData(i);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 根据type值查询字典详情
     * @param type
     * @author CT
     * @return
     */
    @RequestMapping(value = "V100/selectDrugDict",method ={RequestMethod.GET,RequestMethod.POST})
    public String selectPingciV100(String type){
        Message mes = new Message();
        List dictList = DictUtils.getDictList(type);
        mes.setData(dictList);
        return SecurityUtil.encryptMsgData(mes);
    }
}
